<?php
namespace Components\Account\User;

class Logic {
  private function getPaginate($builder, $limit, $page) {
    return ['code' => 0, 'models'=>(new \Phalcon\Paginator\Adapter\QueryBuilder([
      'builder' => $builder,
      'limit' => $limit,
      'page' => $page
    ])) -> getPaginate()];
  }

  public function createBuilder($parameters) {
    $builder = Adapter::getDI('modelsManager') -> createBuilder()
      -> from([
        'account'  => __NAMESPACE__ . '\\Model',
        'account_info' => __NAMESPACE__ . '\\Info\\Model',
      ]);

    if(isset($parameters['conditions'])) {
      foreach($parameters['conditions'] as $m => $conditions) {
        foreach($conditions as $k => $v) {
          if(!empty($v)) {
            $builder -> andWhere($m . '.' . $k . ' = "' . $v . '"');
          }
        }
      }
    }

    return $builder;
  }

  public function pager($parameters = [], $limit = 10, $page = 1) {
    $limit = abs($limit);
    if($limit <= 0) {
      $limit = 10;
    }

    $page = abs($page);
    if($page <= 0) {
      $page = 1;
    }

    $res = $this -> getPaginate(
      $this -> createBuilder($parameters)
        -> andWhere('account.user_id = account_info.user_id')
        -> orderBy('account.user_id DESC'),
      $limit,
      $page
    );

    $modelsItmes = [];
    foreach($res['models'] -> items as $k => $v) {
      //unset($v['account'] -> password);
      unset($v['account'] -> safe_password);

      $modelsItmes[$k]['account'] = $v['account'] -> toArray();
      $modelsItmes[$k]['account_info'] = $v['account_info'] -> toArray();

      if($v['account'] -> promotion_user_id == 0) {
        $modelsItmes[$k]['p_account']['user_code'] = '无';
      } else {
        $pUser = Model::findFirst('user_id = ' . $v['account'] -> promotion_user_id);
        if(!$pUser) {
          $modelsItmes[$k]['p_account']['user_code'] = '无';
        } else {
          $modelsItmes[$k]['p_account']['user_code'] = $pUser -> user_code;
        }
      }

      $wallet = Wallet\Model::findFirst('user_id = ' . $v['account'] -> user_id);
      if(!$wallet) {
        $modelsItmes[$k]['account_wallet'] = [];
      } else {
        $modelsItmes[$k]['account_wallet'] = $wallet;
      }
    }

    $res['models'] -> items = $modelsItmes;
    return $res;
  }

  public function findFirstById($id) {
    if(!$id) { return ['code' => -1, 'message' => '此数据不存在或已删除!']; }

    $builder = $this -> createBuilder([])
      -> andWhere('account.user_id = account_info.user_id AND account.user_id = ' . $id)
      -> getQuery() -> getSingleResult();
    unset($builder -> account -> password);
    unset($builder -> account -> safe_password);

    if(!$builder) { return ['code' => -1, 'message' => '此数据不存在或已删除!']; }

    $model = $builder -> toArray();
    if($builder -> account -> service_user_id != 0) {
      $tm = Adapter::getServiceInstance() -> findFirst([
        'conditions' => 'user_id = ' . $builder -> account -> promotion_user_id,
        'columns' => 'user_code, real_name'
      ]);
      if($tm['code'] == 0) { $model['p_account'] = $tm['model']; }
      unset($tm);
    }

    if($builder -> account -> service_user_id != 0) {
      $tm = Adapter::getServiceInstance() -> findFirst([
        'conditions' => 'user_id = ' . $builder -> account -> service_user_id,
        'columns' => 'user_code, real_name'
      ]);
      if($tm['code'] == 0) { $model['s_account'] = $tm['model']; }
      unset($tm);
    }

    if($builder -> account -> service_user_id != 0) {
      $tm = Adapter::getServiceInstance() -> findFirst([
        'conditions' => 'user_id = ' . $builder -> account -> node_user_id,
        'columns' => 'user_code, real_name'
      ]);
      if($tm['code'] == 0) { $model['n_account'] = $tm['model']; }
      unset($tm);
    }

    return ['code' => 0, 'model' => $model];
  }
}
